package com.study.servlet;

import com.alibaba.fastjson.JSON;
import com.study.R;
import com.study.dto.AuthDto;
import com.study.service.AdminService;
import com.study.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PackgeName: com.study.servlet
 * @ClassName: LoginServlet
 * @Author: 严志祥
 * Date: 2022-05-11 19:36
 * project name: web_project_medal
 * @Version:
 * @Description:
 */
@WebServlet("/login.let")
public class LoginServlet extends HttpServlet {
    private AdminService as = new AdminServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("adminName");
        String password = req.getParameter("adminPassword");
        R<?> r = R.ok();
        try{
            AuthDto auth = as.login(username,password);
            HttpSession session = req.getSession();
            session.setAttribute("auth",auth);
        }catch (Exception e){
            r = R.error().setMessage(e.getMessage());
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().print(JSON.toJSONString(r));
        resp.flushBuffer();
    }
}
